<?
include_once($_SERVER['DOCUMENT_ROOT']."/lucy/config.php");

class lucy_feelings{
	private $feeling;
	private $add_feeling;

	function lucy_feelings(){
		$this->get();
		$this->add_feeling = $this->feeling;
	}
	
	function add_feeling($add_feeling){//	saglabā 	jaunu
		if($this->save($add_feeling)){
			return $this->get();
		}
	}
	
	function change($positive){//	daļēji izmaina par +/- 1
		//if($this->feeling > 1 || $this->feeling < 5){
			$this->add_feeling($this->feeling + $positive);
		//}
	}
	
	function show(){
		return $this->feeling;
	}
	
	function get(){
		if(!$dbcon){include(INC.'dbcon.php');}
		$today_morning = mktime(5, 0, 0, date("m"), date("d"), date("Y"));
		$now = time();
		$sql = "SELECT AVG(`feeling`) AS `feeling_now` FROM `lucy_feelings` WHERE UNIX_TIMESTAMP(`date`) BETWEEN '$today_morning' AND '$now'";
		$rez = mysql_query($sql, $dbcon);
		if(mysql_num_rows($rez)){
			$feel = mysql_fetch_assoc($rez);
			$this->feeling = $feel['feeling_now'];
			return round($this->feeling);
		}else{
			return 0;
		}
	}	

	function get_precize(){
		if(!$dbcon){include(INC.'dbcon.php');}
		$today_morning = mktime(5, 0, 0, date("m"), date("d"), date("Y"));
		$now = time();
		$sql = "SELECT AVG(`feeling`) AS `feeling_now` FROM `lucy_feelings` WHERE UNIX_TIMESTAMP(`date`) BETWEEN '$today_morning' AND '$now'";
		$rez = mysql_query($sql, $dbcon);
		if(mysql_num_rows($rez)){
			$feel = mysql_fetch_assoc($rez);
			$this->feeling = $feel['feeling_now'];
			return $this->feeling;
		}else{
			return 0;
		}
	}	
	
	function set_feeling($add_feeling){
		if($add_feeling > 0 AND $add_feeling <= 5){
			$this->add_feeling = $add_feeling;
			return 1;
		}else{
			return 0;
		}
	}
	
	function save($add_feeling = ""){
		if($add_feeling != ""){
			$this->set_feeling($add_feeling);
		}
		if($this->add_feeling != 0){
			if(!$dbcon){include(INC.'dbcon.php');}
			$sql = "INSERT INTO `lucy_feelings` (`feeling`, `date`) VALUES ('{$this->add_feeling}', NOW())";
			$rez = mysql_query($sql, $dbcon);
			return ($rez) ? 1 : 0;
		}else{
			return 0;
		}
	}
	

}